package com.toutoushidao.ssm.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.ontio.OntSdk;
import com.github.ontio.network.exception.ConnectorException;
import com.github.ontio.sdk.exception.SDKException;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class HashQueryTest {
    //    0db8d185e4c633b9f2e6ee620fb86c213a689aa7a8fc81805a4d04dd6a4487d3
//    fade22d7d153eb9a0d862c87d1360f595ed86efecaa80d41333d049a46774b53
    public static void main(String[] args) throws SDKException, ConnectorException, IOException {
        String result = "";
        String transaction = "aa0723c25fa5ff531e7ad9c7d7fd3958ed9913668e559dc3f8d1b91bb1067cb2";
        OntSdk ontQuery = OntSdk.getInstance();
        ontQuery.setRestful("http://dappnode1.ont.io:20334");
        ontQuery.setDefaultConnect(ontQuery.getRestful());
        Object tradeDetail = ontQuery.getConnect().getSmartCodeEvent(transaction);//.neovm().oep4().setContractAddress(OWNER_HASH)
        int awardIndex;
        if (tradeDetail == null) {
            awardIndex = 0;// 查询失败时, 等同于开到了空气
        } else {
            JSONObject jsonObject = JSONObject.parseObject(tradeDetail.toString());
            JSONArray notify = (JSONArray) jsonObject.get("Notify");
            JSONObject statesJsonObject = notify.getJSONObject(1);
            JSONArray states = (JSONArray) statesJsonObject.get("States");
            String awardHex = states.getString(1);
            awardIndex = 0xff & Integer.parseInt(awardHex);
        }
        Map<Integer, String> awardMap = new HashMap<>();
        awardMap.put(0, "空气");// NULL - 20%
        awardMap.put(1, "复活卡");// 复活卡 - 40%
        awardMap.put(2, "1.00");// 1 ONG - 0.4%
        awardMap.put(3, "0.80");// 0.8 ONG - 1.6%
        awardMap.put(4, "0.10");// 0.1 - 8%
        awardMap.put(5, "0.09");// 0.09 8%
        awardMap.put(6, "0.07");// 0.07 6%
        awardMap.put(7, "0.03");// 0.03 8%
        awardMap.put(8, "0.01");// 0.01 8%
        String award = awardMap.get(awardIndex);
        Map<String, String> resultMap = new HashMap<>();
        resultMap.put("type", "复活卡");
        resultMap.put("award", award);
    }
}
